package com.nit.store.web;


import com.nit.store.ex.ServiceException;
import lombok.Data;

import java.io.Serializable;

@Data
public class JsonResult<T> implements Serializable {
    private Integer state;//业务返回码
    private String message;//错误消息
    private T data;//返回的数据

    private JsonResult(){}

    public static JsonResult<Void> ok(){
        JsonResult<Void> jsonResult = new JsonResult<>();
        jsonResult.state = State.OK.getValue();
        return jsonResult;
    }
    public static<T> JsonResult<T> ok(T data){
        JsonResult<T> jsonResult = new JsonResult<>();
        jsonResult.state = State.OK.getValue();
        jsonResult.data = data;
        return jsonResult;
    }
    public static JsonResult<Void> fail(State state, String message){
        JsonResult<Void> jsonResult = new JsonResult<>();
        jsonResult.state = state.getValue();
        jsonResult.message = message;
        return jsonResult;
    }
    public static JsonResult<Void> fail(ServiceException e){
        return fail(e.getState(),e.getMessage());
    }

    public enum State{
        OK(2000),
        ERR_NOT_ACCEPTABLE(40600),// ERR_NOT_ACCEPTABLE表示 被限制的操作
        ERR_FORBIDDEN(40300), //  ERR_FORBIDDEN表示被禁止的用户
        ERR_BAD_REQUEST(40000), // ERR_BAD_REQUEST表示请求参数验证格式失败
        ERR_CONFLICT(40900),  //ERR_CONFLICT表示用户名重复
        ERR_INTERNAL_ERROR(50000); //ERR_INTERNAL_ERROR表示代码里面报错


        Integer value;
        State(Integer value){
            this.value = value;
        }
        public Integer getValue() {
            return value;
        }
    }
}
